<!--
 * @Description: 角色管理页面
-->
<template>
  <BaseMainSlot class="patrol-point-manage">
    <template v-slot:btnsBar>
      <ActionBar
        :page-no="175"
        :add-active="addActive"
        :del-active="delActive"
        :cancel-active="cancelActive"
        :func-auth-active="funcAuthActive"
        :func-app-auth-active="funcAppAuthActive"
        :has-func-auth="true"
        :has-app-auth="true"
        :has-save="false"
        :has-edit="false"
        :has-del="false"
        @funcAuth="funcAuthShow"
        @funcAppAuth="funcAppAuthShow"
        @add="add"
        @cancel="cancel"
        @edit="edit"
        @del="del"
        @save="save('formData')"
      />
    </template>
    <template v-slot:searchbox>
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item name="1">
          <template slot="title">
            <div class="select-btns table-btns" @click.stop="changeSelect()">
              <i class="el-icon-circle-check"></i>
              <p v-if="activeName === '1'">收起筛选</p>
              <p v-if="activeName === ''">展开筛选</p>
            </div>
          </template>
          <div class="select-box-content">
            <div class="select-box-content-name">角色名称：</div>
            <el-input
              v-model="roleName"
              clearable
              placeholder="请输入角色名称"
              @change="queryPageList()"
            />
          </div>
        </el-collapse-item>
      </el-collapse>
    </template>
    <template v-slot:content>
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="calc(100% - 50px)"
        @row-click="handleEdit"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="50">
          <template slot-scope="scoped">
            {{ scoped.row.roleId ? scoped.$index + 1 : "" }}
          </template>
        </el-table-column>
        <el-table-column prop="roleName" label="角色名称" />
        <el-table-column prop="description" label="描述" />
        <el-table-column width="200" label="操作">
          <template v-if="scoped.row.roleNo" slot-scope="scoped">
            <div class="table-btns-box">
              <div
                class="table-btns edit-btns"
                @click="edit(scoped.row.roleNo)"
                v-if="judgePermission(175, 'edit')"
              >
                <i class="el-icon-edit-outline"></i>
                <p>修改</p>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
      <div class="el-dialog">
      <el-dialog
        :title="titleText + '角色'"
        :visible.sync="dialogFormVisible"
        width="40%"
        :before-close="cancel"
        :close-on-click-modal="false"
        top="0"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <el-form-item label="角色名称:" prop="roleName" class="w100p">
            <el-input v-model="formData.roleName" clearable />
          </el-form-item>
          <el-form-item label="描述:" prop="description" class="w100p">
            <el-input v-model="formData.description" clearable />
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="cancelbtn" @click="cancel('formData')">
            取 消
          </el-button>
          <el-button
            class="savebtn"
            type="primary"
            @click="save('formData')"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>
      <el-dialog
        title="分配功能权限"
        :visible.sync="dialogDataAuthFormVisible"
        width="40%"
        height="65%"
        :before-close="cancel"
        :close-on-click-modal="false"
        top="0"
      >
        <DataAuthComponent
          v-if="dialogDataAuthFormVisible"
          ref="funcAppAuth"
          :role-no="formData.roleNo"
          :rolestu="rolestu"
          @roles="roles"
        />
        <div slot="footer" class="dialog-footer">
          <el-button class="cancelbtn" @click="cancel"> 取 消 </el-button>
          <el-button
            class="savebtn"
            type="primary"
            @click="saveRole('formData')"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>
      <el-dialog
        title="分配App功能权限"
        :visible.sync="dialogDataAppAuthFormVisible"
        width="40%"
        :close-on-click-modal="false"
        top="0"
      >
        <DataAppAuthComponent
          v-if="dialogDataAppAuthFormVisible"
          ref="funcAuth"
          :role-no="formData.roleNo"
          :rolestu="rolestu"
          @roles="roles"
        />
        <div slot="footer" class="dialog-footer">
          <el-button class="cancelbtn" @click="cancel"> 取 消 </el-button>
          <el-button
            class="savebtn"
            type="primary"
            @click="saveRole('formData')"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>
      </div>
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import DataAuthComponent from "../components/func-auth-component.vue";
import DataAppAuthComponent from "../components/App-auth-component.vue";
import roleManage from "@/api/system-settings/role-manage";
import BaseMainSlot from "@/components/BaseMainSlot/kong.vue";
import { mymixin } from "@/utils/mixins";
import { mapGetters } from "vuex";

export default {
  name: "",
  components: {
    ActionBar,
    Pagination,
    DataAuthComponent,
    DataAppAuthComponent,
    BaseMainSlot,
  },
  mixins: [mymixin],
  data() {
    return {
      // 操作按钮的逻辑及展示数据
      addActive: true,
      editActive: false,
      delActive: false,
      saveActive: false,
      cancelActive: false,
      dataAuthActive: false,
      funcAuthActive: false,
      funcAppAuthActive: false,
      dialogDataAppAuthFormVisible: false,
      // table
      loading: true,
      // 当前页
      currentPage: 1,
      // 每页的数据条数
      pageSize: 10,
      // 总条数
      totalSize: 0,
      // 角色名称
      roleName: "",
      nowpage: 1,
      // 是否能输入
      canInput: false,
      tableData: [],
      rolestu: "",
      formData: {},
      rules: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
      },
      // 选中的数据项数组 ID组成
      roleIdList: [],
      dialogTitle: "",
      dialogFormVisible: false,
      dialogDataAuthFormVisible: false,
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },
  watch: {},
  async created() {
    // 获取表格数据
    await this.getTableDataList();
  },
  mounted() {},
  methods: {
    async getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          roleName: this.roleName,
        },
      };
      roleManage.getPageList(params).then((res) => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data||[];
          // 赋值总条数
          this.totalSize = data.totalElements || 0;
          // 赋值数组
          this.tableData = data.content || [];
          this.addblankmet();
          this.dialogFormVisible = false;
        }
      });
    },
    saveRole() {
      this.rolestu = "add";
    },
    roles() {
      this.rolestu = "";
      this.dialogDataAuthFormVisible = false;
      this.dialogDataAppAuthFormVisible = false;
    },
    // 填充表格空白行
    addblankmet() {
      let havline = this.tableData.length % this.pageSize;
      if (this.tableData.length < this.pageSize) {
        if (havline < this.pageSize) {
          const remain = this.pageSize - havline;
          for (var i = 0; i < remain; i++) {
            this.tableData.push({ indflag: i + this.tableData.length - 1 });
          }
        }
      }
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    handleEdit(row) {
      this.formData = JSON.parse(JSON.stringify(row));
      if (row.roleNo) {
        this.funcAuthActive = true;
        this.funcAppAuthActive = true;
      } else {
        this.resetActionBar();
      }
      // this.openDialog('add');
    },
    handleSelectionChange(list) {
      this.roleIdList = [];
      this.roleIdList = list.map((item) => item.roleNo);
      if (this.roleIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    // add() {
    //   // TODO 添加后 input可输入 add及cancel 1 其余 0
    //   this.canInput = true;
    //   this.addActive = false;
    //   this.editActive = false;
    //   this.delActive = false;
    //   this.saveActive = true;
    //   this.cancelActive = true;
    //   // this.nowpage = 1;
    //   this.dialogFormVisible = true
    //   // roleId置空
    //   // this.formData.roleNo = "";
    // },
    // 初始化操作栏状态
    resetActionBar() {
      this.funcAuthActive = false;
      this.dataAuthActive = false;
      this.assignUserActive = false;
      this.funcAuthActive = false;
      this.funcAppAuthActive = false;
      this.nowpage = 1;
      this.canInput = false;
      this.addActive = true;
      this.delActive = false;
      this.saveActive = false;
      this.cancelActive = false;
      this.editActive = false;
    },
    // 点击功能权限触发的方法
    funcAuthShow(data) {
      // this.openDialog('edit');
      // console.log()
      // this.formData = data;
      this.dialogDataAuthFormVisible = true;
    },
    funcAppAuthShow() {
      this.dialogDataAppAuthFormVisible = true;
    },
    del(e) {
      this.$message.warning("角色不允许删除");
      // roleManage.allmetInfo({roleNoList: [e]}, "delList").then(res => {
      //   if (res.code === 1) {
      //     this.$message.success(res.msg);
      //     this.getTableDataList();
      //   }
      // });
    },
    cancel() {
      this.closeDialog();
      this.formData = {};
      this.funcAuthActive = false;
      this.dataAuthActive = false;
      this.assignUserActive = false;
      this.funcAuthActive = false;
      this.funcAppAuthActive = false;
      this.addActive = true;
      this.dialogDataAuthFormVisible = false;
      this.dialogDataAppAuthFormVisible = false;
      // this.$nextTick(() => {
      //   if (this.formData) {
      //     this.$refs[formData].resetFields();
      //   }
      // })
    },
    save(formData) {
      this.$refs[formData].validate((valid) => {
        if (valid) {
          if (this.formData.roleNo) {
            // 编辑接口
            roleManage.allmetInfo(this.formData, "edit").then((res) => {
              if (res.code === 1) {
                this.$message.success("修改成功！");
                this.formData = {};
                this.getTableDataList();
              }
            });
          } else {
            // 新增接口
            roleManage.allmetInfo(this.formData, "add").then((res) => {
              if (res.code === 1) {
                this.$message.success("新增成功！");
                this.formData = {};
                this.getTableDataList();
              }
            });
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
        if (this.formData.id && this.nowpage === 2) {
          this.$refs.funcAuth.setpermission();
          this.resetActionBar();
        } else if (this.nowpage === 3) {
          this.$refs.assignrole.checkchange();
        }
        this.resetActionBar();
      });
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === "add" ? "新增" : "编辑";
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = "";
    },
  },
};
</script>

<style scoped lang="scss">
@import "~@/styles/variables.scss";
.patrol-point-manage {
  width: 100%;
  height: 100%;
  ::v-deep .el-form-item__content {
    display: flex;
    align-items: center;
  }
  ::v-deep .el-form-item__label {
    min-width: 150px;
  }
  .dialog {
    ::v-deep .el-dialog__title {
      font-weight: bold;
    }
    .qrCode {
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  .common-layout {
    border: 1px solid $borderColor;
    height: calc(100% - #{$actionBarHeight + 10});
    padding: 20px;
    display: flex;
    .left-box {
      width: 50%;
      height: 100%;
      .search-box {
        display: flex;
        align-items: center;
        margin-bottom: 30px;
        .label-input {
          display: flex;
          align-items: center;
          .label {
            width: 80px;
            white-space: nowrap;
          }
        }
        .search {
          display: flex;
          align-items: center;
          img {
            height: 30px;
          }
        }
      }
      .table-box {
        height: calc(100% - 50px);
        position: relative;
        overflow-y: auto;
      }
    }
    .right-box {
      width: 49%;
      height: 100%;
      margin-left: 1%;
      border-left: 1px dashed $borderColor;
      padding-left: 20px;
      .content {
        height: 100%;
        .form {
          overflow-y: auto;
          height: 100%;
          .el-form {
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
          }
          .map {
            width: 100%;
            height: 300px;
          }
        }
      }
    }
  }
}


</style>
